package keter.web.controller._experiment;

import keter.framework.web.util.MediaType;
import keter.model.User;
import keter.web.controller.base.KeterBaseController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * <p>Class       : app.controller.IndexLoginController
 * <p>Descdription: 首页和登录控制逻辑的统一处理
 *
 * @author  gulixing@msn.com
 * @version 1.0.0
 */
@Controller
@RequestMapping("keter/exp")//注意："path"和"path/"是两个不同的映射
public class ExpController extends KeterBaseController {
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(ExpController.class);
   
    /*与 @RequestMapping中的内容保持一致 */
	private static final String path = "keter/exp";
		
    /**
     * <p>
     * Method ：update
     * <p>
     * Description : 进入demo模块首页
     * 
     * @param model
     * @param id
     * @return     @Valid Customer customer,BindingResult binder
     * @author 顾力行-gulx@neusoft.com
     */
	@GetMapping(value=Routes.Index)
    public String index() {
    	logger.info("访问/appName/demo，跳转到/appName/demo/demo_index页面");
        return to_(path)+"index";
    }

    /* 前后台交互功能测试  */
	@GetMapping(value="/getuser", produces = MediaType.APPLICATION_JSON_UTF_8)
    @ResponseBody
    public String getUser() throws Exception {
    	logger.info("获取当前用户..");
        return getCurrentUser().toJson();
    }
    
	@GetMapping(value="/getuserrole", produces = MediaType.APPLICATION_JSON_UTF_8)
    @ResponseBody
    public String getUserRole() throws Exception {
        logger.info("获取当前用户角色..");
        return toJson(getCurrentUser().getRoleList());
    }
    
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping(value="/getusers", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF_8)
    @ResponseBody
    public String getUsers() throws Exception {
    	logger.info("获取全部用户..");
        return toJson(User.dao.findAll());
    }
    
    @GetMapping(value="/getusers/{pageNum}/{pageSize}", produces = MediaType.APPLICATION_JSON_UTF_8)
    @ResponseBody
    public String getUsers(@PathVariable int pageNumber,@PathVariable int pageSize) throws Exception {
    	logger.info("第{}页，每页{}条用户",pageNumber,pageSize);
    	return toJson(User.dao.findPaged(1, 1));
    }
    
    @GetMapping(value="/exception", produces = MediaType.APPLICATION_JSON_UTF_8)
    @ResponseBody
    public String exception() throws Exception { 
        logger.info("进入错误页面");
    	return toJson(User.dao.findPaged(0, 0));
    }
    
    @GetMapping(value="/pagenotfound")
    public String pageNotfound() {
        logger.info("进入404页面");
        return "pagenotfound";
    }
    
    @GetMapping(value="/demouser")
    public String userDemoIndex() {
        return to_(path)+"user";
    }
}